import axios from './api.request.js'
// import 'xe-utils' // vxeTable 通用库
import VXETable from 'vxe-table'
// import {Loading} from 'element-plus'; 
// import JSONbig from 'json-bigint'
export default {
  request:function(options){
    try {
      // Call the appropriate method based on options.method
      if (this[options.method] && typeof this[options.method] === 'function') {
        return this[options.method](options.url, options.data, options.params)
      } else {
        throw new Error(`Method ${options.method} not found`)
      }
    } catch (error) {
     VXETable.modal.message({message:'请求方式不存在', status:'error' })
     return Promise.reject(error)
    }
  },
  get: function (url, data, params) {
    let _self = this
    return new Promise((resolve, reject) => {
      axios.request({
        url: url,
        method: 'get',
        params: params || {},
        data: data || {}
      }).then(res => {
        _self.requestSuccessHandler(res, resolve, reject)
      }).catch(err => {
        _self.requestErrorHandler(err, resolve, reject)
      })
    })
  },
  post: function (url, data, params) {
   // debugger
    let _self = this
    return new Promise((resolve, reject) => {
      axios.request({
        url: url,
        method: 'post',
        params: params || {},
        data: data || {}
      }).then(res => {
       // debugger
        _self.requestSuccessHandler(res, resolve, reject)
      }).catch(err => {
       // debugger
        _self.requestErrorHandler(err, resolve, reject)
      })
    })
  },
  download_post: function (url, data, params) {
   // debugger
    let _self = this
    return new Promise((resolve, reject) => {
      axios.request({
        url: url,
        method: 'POST',
        responseType: 'blob', // 下载文件标识
        params: params || {},
        data: data || {}
      }).then(res => {
        // _self.requestSuccessHandler(res, resolve, reject)
         resolve(res)
      }).catch(err => {
        //_self.requestErrorHandler(err, resolve, reject)
        reject(err)
      })
    })
  },
  download_get: function (url, data, params) {
    // debugger
     let _self = this
     return new Promise((resolve, reject) => {
       axios.request({
         url: url,
         method: 'get',
         responseType: 'blob', // 下载文件标识
         params: params || {},
         data: data || {}
       }).then(res => {
         // _self.requestSuccessHandler(res, resolve, reject)
          resolve(res)
       }).catch(err => {
         //_self.requestErrorHandler(err, resolve, reject)
         reject(err)
       })
     })
   },
  formPost: function (url, data, params) {
    let _self = this
    return new Promise((resolve, reject) => {
      axios.request({
        url: url,
        method: 'post',
       // headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
      //  'Accept':'*/*',
      //       'Content-Type': 'application/x-www-form-urlencoded'
       headers: {
        'Accept':'*/*',
        'Content-Type': 'multipart/form-data',
        withCredentials: true,
      },
        params: params || {},
        data: data || {}
      }).then(res => {
        _self.requestSuccessHandler(res, resolve, reject)
      }).catch(err => {
        _self.requestErrorHandler(err, resolve, reject)
      })
    })
  },
  update: function (url, data, params) {
   // debugger
    let _self = this
    this.openFullScreen(true)
    return new Promise((resolve, reject) => {
      axios.request({
        url: url,
        method: 'post',
        params: params || {},
        data: data || {}
      }).then(res => {
       // debugger
          _self.openFullScreen(false)
         _self.requestSuccessHandlerByUpdate(res, resolve, reject)
      }).catch(err => {
       // debugger
         _self.openFullScreen(false)
        _self.requestErrorHandler(err, resolve, reject)
      })
    })
  },
  requestSuccessHandlerByUpdate (res, resolve, reject) {
   // debugger
    if (!res) {
      reject(false)
      return
    }
   // debugger
    if (res.data.success) { // 返回数据成功
      // resolve(res.data.result)
       resolve(res.data.data)
      let successfulExecution =  "操作成功"
      VXETable.modal.message({message:successfulExecution, status:'success' })
    } else {
      //debugger
      let requestException =  "请求业务异常，请联系系统管理员-001"
      let msg = res.data.message.content == null || res.data.message.content == '' ? requestException : res.data.message.content
      this.showErrMessage(msg)
      reject(msg)
    }
  },
  requestSuccessHandler (res, resolve, reject) {
    //debugger
    if (!res) {
      reject(false)
      return
    }
    if (res.data.success) { // 返回数据成功
        // resolve(res.data.data)
        if(!!res.data.data.Success){
           resolve(res.data.data)
        }else{
          // 统一处理失败结果，消息提示
          resolve(res.data.data)
          this.showErrMessage(!!res.data.data.Content?res.data.data.Content:'返回结果失败，请检查！！')
        }
    } else {
      let requestException =  "请求业务异常，请联系系统管理员"
      let msg = res.data.message.content == null || res.data.message.content == '' ? requestException: res.data.message.content
      this.showErrMessage(msg)
      reject(msg)
    }
  },
  showErrMessage(msg){
       clearTimeout(this.showMessageTimer)
       this.showMessageTimer = setTimeout(() => {
           VXETable.modal.message({message:msg, status:'error' })
       }, 300);
  },
  requestErrorHandler (err, resolve, reject) {
    if (!!err) {
      this.showErrMessage(err)
      reject(err)
    }
  },
  openFullScreen(showOrNot=false) {
    // let dataLoading = "数据加载中,请稍等..."
    // const loadingObj = Loading.service({
    //   lock: showOrNot,
    //   text: dataLoading,
    //   spinner: 'el-icon-loading',
    //   background: 'rgba(0, 0, 0, 0.7)'
    // });
    // if(!showOrNot){
    //     loadingObj.close();
    // }
  },
}
